/**
 * Created by Administrator on 2018/4/11 0011.
 */
const models = require('../models')

const { User } = models
const { Address } = models

exports.create = function (userId, obj, callback) {
  User.findById(userId, (err, user) => {
    console.log(user, 'user')
    const address = new Address(obj)
    user.address.push(address)
    console.log(address._id)
    user.save((err, userinfo) => {
      if (err) return callback(err)
      callback(err, address, userinfo)
    })
  })
}
exports.remove = function (userId, addressId, callback) {
  User.findById(userId, (err, user) => {
    const address = user.address.id(addressId)
    address.remove()
    user.save(callback)
  })
}
exports.update = function (userId, obj, callback) {
  User.findById(userId, (err, user) => {
    if (err) return callback(err)
    const address = user.address.id(obj._id)
    address.set(obj)
    user.save((err, subUser) => {
      if (err) return callback(err)
      const addr = subUser.address.find((item) => item._id === address._id)
      callback(err, addr, subUser)
    })
  })
  // User.findOneAndUpdate(
  //  {"_id": userId, 'address._id': obj._id},
  //   { $set: {
  //     'address.$':obj
  //   }},
  //   callback
  // )
}

exports.findAllAddress = function (userId, callback) {
  User.findOne({ _id: userId }, { address: 1 }, callback)
}
